package com.qn.example.controller;

import com.qn.example.pojo.Category;
import com.qn.example.pojo.Page;
import com.qn.example.pojo.R;
import com.qn.example.service.impl.CategoryServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 21276
 * @version 1.0
 * @date 2023/5/4 14:16
 *
 * 菜品分类层
 */

@RestController
@Slf4j
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {

    @Resource
    private CategoryServiceImpl categoryService;

    /**
     * 分页
     */
    @GetMapping("/page")
    public R page(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        //调用业务层的获取每一页对象的方法
        Page<Category> pageBean = categoryService.findByPage(page,pageSize);
        return  R.success(pageBean);
    }

    /**
     * 新增菜品及套餐
     */
    @PostMapping
    public R save(@RequestBody Category category, HttpSession session) {
        //获取当前登录的用户id
        Long uId = (Long) session.getAttribute("user");
        //添加创建用户(补全创建人和修改人)
        category.setCreateUser(uId);
        category.setUpdateUser(uId);

        try {
            categoryService.save(category);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(category.getName() + "已经存在！");
        }

        //将R对象返回给页面
        return R.success("添加成功");
    }

    /**
     * 修改菜品信息 或者 套餐信息
     */

    @PutMapping
    public R  updateEmp(@RequestBody Category category,HttpSession session){
        System.out.println("修改的id："+category.getId());
        System.out.println("修改的名字："+category.getName());
        System.out.println("修改的排序："+category.getSort());
        //补全修改人
        //1.先从session中取出当前登录的用户
        Long empId = (Long) session.getAttribute("employee");
        //2. 补全员工的创建人与修改人
        category.setUpdateUser(empId);
        System.out.println("修改人："+category.getUpdateUser());
        categoryService.updateEmp(category);
        return R.success("修改成功");
    }

    //删除
    @DeleteMapping
    public R delete(@RequestParam("id") Long id){
        System.out.println("删除的id："+id);
        categoryService.delete(id);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R foodClass(Integer type){
        List<Category> categories = categoryService.foodClassNames(type);
        return R.success(categories);
    }

    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        //读取静态资源下的文件
        InputStream is = CategoryController.class.getResourceAsStream("/excels/category.xlsx");
        //创建工作溥
        Workbook workbook = new XSSFWorkbook(is);
        //获取工作簿中的第一张表
        Sheet sheet = workbook.getSheetAt(0);
        //获取样式行
        Row row = sheet.getRow(1);
        //创建一个存储样式的集合
        List<CellStyle> cellStyles = new ArrayList<>();

        //遍历行获取每一个单元格的样式存储到集合中
        for (int i = 1; i <= row.getPhysicalNumberOfCells() ; i++) {
            //获取单元格的样式
            Cell cell = row.getCell(i);
            CellStyle cellStyle = cell.getCellStyle();
            cellStyles.add(cellStyle);
        }
        //获取类别的所有数据
        List<Category> categories = categoryService.findAllCategory();

        //创建时间日期格式化的对象
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");

        //遍历所有数据的集合
        for (int i = 0; i < categories.size(); i++) {
            //获取每一行的具体数据
            Category category = categories.get(i);
            //创建行
            row = sheet.createRow(i+1);
            //创建单元格
            Cell cell = row.createCell(1);
            cell.setCellStyle(cellStyles.get(0));
            cell.setCellValue(category.getType());
            //创建单元格
            cell = row.createCell(2);
            cell.setCellStyle(cellStyles.get(1));
            cell.setCellValue(category.getName());

            cell = row.createCell(3);
            cell.setCellStyle(cellStyles.get(2));
            cell.setCellValue(category.getSort());


            cell = row.createCell(4);
            cell.setCellStyle(cellStyles.get(3));
            cell.setCellValue(dateTimeFormatter.format(category.getCreateTime()));

            cell = row.createCell(5);
            cell.setCellStyle(cellStyles.get(4));
            cell.setCellValue(dateTimeFormatter.format(category.getUpdateTime()));

        }

        //将创建出来的数据表以附件的形式
        //设置响应头
        response.setHeader("content-disposition","attachment;filename=category.xlsx");
        //回写数据
        workbook.write(response.getOutputStream());
    }
}
